package lumis.service.chatroom;

import java.util.List;

import org.hibernate.Session;
import org.w3c.dom.Node;

import lumis.collaboration.chat.ChatManagerFactory;
import lumis.content.service.ContentService;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.dao.hibernate.ITransactionHibernate;
import lumis.portal.serviceinstance.ServiceInstanceConfig;
import lumis.util.ITransaction;
import lumis.util.XmlUtil;

/**
 * Chat Room Service
 *
 * @version $Revision: 4482 $ $Date: 2006-09-18 18:33:56 -0300 (Mon, 18 Sep 2006) $
 * @since 4.0.6
 */
public class ChatRoomService extends ContentService
{
	/**
	 * before deleting chat rooms, must clear the corresponding chat manager conversations
	 */
	protected void deleteData(SessionConfig sessionConfig, ServiceInstanceConfig config, List<String> itemIdsTemp, Node currentSourceDefinitionNode, ITransaction transaction) throws PortalException
	{
		ITransactionHibernate transactionHib = (ITransactionHibernate) transaction;
		Session session = transactionHib.getSession();

		String sourceId = XmlUtil.readAttributeString("id", currentSourceDefinitionNode);
		
		if("chatRoom".equals(sourceId))
		{
			for(String chatRoomId : itemIdsTemp)
			{
				ChatRoomConfig chatRoomConfig = (ChatRoomConfig)session.get(ChatRoomConfig.class,chatRoomId);
				
				// notify chat manager to delete the conversation
				ChatManagerFactory.getChatManager().deleteConversation(sessionConfig, chatRoomConfig.getChatConversationId(), transaction);
			}
		}
		
		super.deleteData(sessionConfig, config, itemIdsTemp, currentSourceDefinitionNode, transaction);
	}

}
